﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Sysgeprava.Entities;
using System.Data;

namespace Sysgeprava.Repository
{
    public class HistControlRepository:MasterRepository,IHistControlRepository
    {
        public List<HIST_CONTROL> GetAllFromHistControl()
        {
            var query = from c in context.HIST_CONTROLs.Include("CONTROL").Include("BOVINO").Include("PAJILLA")
                        select c;
            return query.ToList();
        }

        public HIST_CONTROL GetAllFromHistControlById(int id)
        {
            var query = from c in context.HIST_CONTROLs.Include("CONTROL").Include("BOVINO").Include("PAJILLA")
                        where c.Id == id
                        select c;
            return query.SingleOrDefault();
        }

        public void AddHistControl(HIST_CONTROL histControl)
        {
            histControl.BOVINO = null;
            histControl.CONTROL = null;
            histControl.PAJILLA = null;

            context.HIST_CONTROLs.Add(histControl);
            context.SaveChanges();
        }

        public void UpdateHistControl(HIST_CONTROL histControl)
        {
            var histControlDB = context.HIST_CONTROLs.Find(histControl.Id);

            histControlDB.Fecha = histControl.Fecha;
            histControlDB.IdBovino = histControl.IdBovino;
            histControlDB.IdControl = histControl.IdControl;
            histControlDB.IdPajilla = histControl.IdPajilla;
            histControlDB.Observacion = histControl.Observacion;

            context.Entry(histControlDB).State = EntityState.Modified;
            context.SaveChanges();
        }
    }
}
